------------------------------------------------------------------------------------------------------------------------------------------------
-- -- tasks base lib
local function language_change_say(theNPC)
    theNPC.CHAT_TABLE = {}
    pcall(function()
        -- theNPC.npc_base_lib:Get_Language() == "ch"
        theNPC.CHAT_TABLE = TUNING.NPC_CHAT_TABLE[ theNPC.npc_base_lib:Get_Language() ] ["npc_item_risk_taker_tent"] or {}
    end)
end

-------------------------------------------------------------------------------------------------------------------------------------------------
local function Item_Get_Event_Init(theNPC)
    theNPC:ListenForEvent("itemget",function(theNPC,_table)
        if _table and _table.item then
            if  _table.item.prefab == "npc_item_transform_compass" then
                theNPC.event_step = 0
                theNPC.sg:RemoveStateTag("npc_waiting")
                theNPC:AddTag("INLIMBO")
                theNPC.npc_base_lib:Hide_Chat_Bubble()
            end
        end
    end)
    -------------------------------------------------------------------------
    local function ShouldAcceptItem(inst, item)
        if item and item.prefab == "npc_item_transform_compass" then
            return true
        end
        return false
    end
    local function OnGetItem(inst, giver, item)
        print("OnGetItem ,Giver :",giver,item)
    end
    local function abletoaccepttest(inst,item,giver)
        if item and item.prefab == "npc_item_transform_compass" then
            return true
        end
        return false
    end
    if theNPC.components.trader == nil then
        theNPC:AddComponent("trader")
    end
    theNPC.components.trader:SetAcceptTest(ShouldAcceptItem)
    theNPC.components.trader.onaccept = OnGetItem
    theNPC.components.trader.deleteitemonaccept = false
    theNPC.components.trader:SetAbleToAcceptTest(abletoaccepttest)
    -------------------------------------------------------------------------
end
local function trader_event_init(theNPC)
    theNPC.AnimState:PlayAnimation("emote_loop_sit2",true)
    Item_Get_Event_Init(theNPC)
    -----------------------------------------------------------
    ---- sg hook
        theNPC.sg:AddStateTag("npc_waiting")
        theNPC.sg.GoToState__old = theNPC.sg.GoToState
        theNPC.sg.GoToState = function(self,statename,...)
            if self:HasStateTag("npc_waiting") then
                theNPC.AnimState:PlayAnimation("emote_loop_sit2",true)
                return
            end
            self:GoToState__old(statename,...)
        end
    -----------------------------------------------------------
    theNPC:AddComponent("playerprox")
    theNPC.components.playerprox:SetDist(10, 13)
    theNPC.components.playerprox:SetOnPlayerNear(function(_,player)
        theNPC.npc_base_lib:Show_Chat_Bubble({
            image = "compass",
            sound_off = true,
        })
        -- local player = theNPC:GetNearestPlayer(true)
        if player and player.userid and player.___npc_item_risk_taker_tent__wisper_task == nil then
            player.npc_base_lib:Wisper(theNPC.CHAT_TABLE.wisper)
            player.___npc_item_risk_taker_tent__wisper_task = player:DoTaskInTime(10,function()
                player.___npc_item_risk_taker_tent__wisper_task = nil
            end)
        end
    end)
    theNPC.components.playerprox:SetOnPlayerFar(function()
        theNPC.npc_base_lib:Hide_Chat_Bubble()
    end)
    -----------------------------------------------------------
end
local function GiveSupplies(theNPC)
    local player = theNPC:GetNearestPlayer(true)
    if player then
        player.npc_base_lib:GiveItemByName("npc_item_goldnugget_coin",math.random(2,4))
    end
end
local function NPC_Leave(theNPC)
    local theplayer = theNPC.__link_player
    SpawnPrefab("npc_fx_spawn"):PushEvent("Set",{pt = Vector3(theNPC.Transform:GetWorldPosition())})
    ReplacePrefab(theplayer, "portabletent")
    theNPC:Remove()
end
local function NPC_TASKS_INIT(inst)
    local theNPC = inst.entity:GetParent()
    language_change_say(theNPC)
    trader_event_init(theNPC)
    theNPC.npc_event = inst.prefab
end

local function NPC_TASKS(inst)
    local theNPC = inst.entity:GetParent()
    if TheWorld.state.isnight  or TheWorld:HasTag("cave") then
        theNPC.npc_base_lib:Try_2_Set_Light(true)
    else
        theNPC.npc_base_lib:Try_2_Set_Light(false)
    end
    local theplayer = theNPC.__link_player
    if theplayer == nil or theplayer:IsValid() == false then
        inst:Remove()
        theNPC:Remove()
        print("Warning : theNPC.__link_player == nil")
        return
    end

    if theNPC.event_step == nil then
        return        
    elseif theNPC.event_step == 0 then
        if theNPC.npc_everything_data:Add("wait time .00",1) >= 6 then
            theNPC.event_step = theNPC.event_step + 1
        end
    elseif theNPC.event_step == 1 then
        theNPC.npc_base_lib:Say(theNPC.CHAT_TABLE.thanks)
        theNPC.event_step = theNPC.event_step + 1
    elseif theNPC.event_step == 2 then
        if theNPC.npc_everything_data:Add("wait time 00",1) >= 6 then
            theNPC.event_step = theNPC.event_step + 1
        end
    elseif theNPC.event_step == 3 then
        local player = theNPC:GetNearestPlayer(true)
        if player then
            theNPC.npc_base_lib:FaceToInst(player)
        end
        theNPC.npc_base_lib:Action_Give()
        theNPC.event_step = theNPC.event_step + 1
    elseif theNPC.event_step == 4 then
        GiveSupplies(theNPC)
        theNPC.npc_base_lib:Say(theNPC.CHAT_TABLE.it_is_for_you)
        theNPC.event_step = theNPC.event_step + 1
    elseif theNPC.event_step == 5 then
        if theNPC.npc_everything_data:Add("wait time 01",1) >= 6 then
            theNPC.event_step = theNPC.event_step + 1
        end          
    elseif theNPC.event_step == 6 then
        NPC_Leave(theNPC)
    end
end
--------------------------------------------------------------------------------------------------------------------------------------------------

local function OnAttached(inst,target) -- when the debuff on or player is newSpawn(or through the cave)
    inst.entity:SetParent(target.entity)
    inst.Network:SetClassifiedTarget(target)
    local NPC = inst.entity:GetParent()
    local flag ,error_code= pcall(function(inst,target)
        if TheWorld.ismastersim then

            NPC_TASKS_INIT(inst)


        end
    end,inst,target)
    if flag == false  then
        print("Error : OnAttached(inst,target) ",inst.prefab)
        print(error_code)
        NPC:Remove()
    end
end

local function OnDetached(inst) -- When eliminating from the outside
    local player = inst.entity:GetParent()   
    -- print(" FROM OUSIDE REMOVE ")
    
    -- inst:Remove()
end

local function ExtendDebuff(inst)
    -- inst.countdown = 3 + (inst._level:value() < CONTROL_LEVEL and EXTEND_TICKS or math.floor(TUNING.STALKER_MINDCONTROL_DURATION / FRAMES + .5))
end



local function OnUpdate(inst,ismastersim)
    local NPC = inst.entity:GetParent()
    local flag,error_code = pcall(function(inst,ismastersim)
        -- local NPC = inst.entity:GetParent()

        NPC_TASKS(inst)


    end,inst,ismastersim)
    if flag == false then
        print("Error : OnUpdate(inst,ismastersim) ,step:",NPC.event_step ,inst.prefab)
        print(error_code)
        NPC:Remove()
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()

    inst:AddTag("CLASSIFIED")


    -- inst:DoPeriodicTask(1, OnUpdate, nil, TheWorld.ismastersim)  -- in one seconds
    inst:DoPeriodicTask( 0.5, OnUpdate, nil, TheWorld.ismastersim)  -- in 0.5 seconds

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)
    inst.components.debuff.keepondespawn = false -- 是否保持debuff 到下次登陆
    -- inst.components.debuff:SetDetachedFn(inst.Remove)
    inst.components.debuff:SetDetachedFn(OnDetached)
    -- inst.components.debuff:SetExtendedFn(ExtendDebuff)
    -- ExtendDebuff(inst)


    return inst
end

return Prefab("npc_event_risk_taker", fn)
